package com.ly.hotel.spainterface.nm;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.spainterface.nm.bean.NmXFActionBean;
import com.ly.hotel.spainterface.nm.event.XFActionEvent;

import java.util.List;

import de.greenrobot.event.EventBus;

public class NmXFActionAdapter extends BaseAdapter {
	Context mContext;
	LayoutInflater mInflater;
	private List<NmXFActionBean> xtAction;
	private int selectPosition = -1;//用于记录用户选择的变量

	public NmXFActionAdapter(Context c, List<NmXFActionBean> xAction) {
		xtAction = xAction;
		mContext = c;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return xtAction.size();
	}

	@Override
	public Object getItem(int position) {
		return xtAction.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setSelectPosition(int position){
		XFActionEvent e;
		if(position == selectPosition){
			selectPosition = -1;
			e = new XFActionEvent(null,xtAction.get(0).getType());
		}else {
			selectPosition = position;
			e = new XFActionEvent(xtAction.get(position).getName(),xtAction.get(position).getType());
		}
		EventBus.getDefault().post(e);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.nm_xf_radio_button,parent,false);
			viewHolder = new ViewHolder();
			viewHolder.name = convertView.findViewById(R.id.id_name);
			viewHolder.select = convertView.findViewById(R.id.id_select);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder)convertView.getTag();
		}
		viewHolder.name.setText(xtAction.get(position).getName());
		if(selectPosition == position){
			viewHolder.select.setChecked(true);
		}
		else{
			viewHolder.select.setChecked(false);
		}
		return convertView;
	}

	static class ViewHolder {
		TextView name;
		RadioButton select;
		NmXFActionBean v;
	}
}
